package com.itheima.config;

import com.itheima.common.AuthHandlerInterceptor;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * @author robin
 * @version 1.0
 * WebMvcConfigurationSupport： 对springmvc底层很多配置进行封装
 */
@Configuration
@ComponentScan("com.itheima")
public class SpringmvcConfig extends WebMvcConfigurationSupport {

    //视图解析器：对逻辑视图进行解析，/WEB-INF/pages/success.jsp
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        registry.jsp("/WEB-INF/pages/", ".jsp");
        super.configureViewResolvers(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
        super.addResourceHandlers(registry);
    }

    //@TODO 配置拦截器:添加自定义拦截器

    /**
     * @param registry
     * @WebFilter("/*") 回顾：MyFilter
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //所有请求都可以被当前的拦截器进行拦截过滤 /hello  /hello/aa
        //excludePathPatterns("/error") 排除/error请求不拦截
        registry.addInterceptor(new AuthHandlerInterceptor()).addPathPatterns("/*").excludePathPatterns("/error");
        super.addInterceptors(registry);
    }
}
